package com.demo.java.http.client;

import org.apache.hadoop.yarn.webapp.MimeType;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;

public class ClientRequestTest {

    public static void main(String[] args) throws IOException, URISyntaxException {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(1000).setSocketTimeout(3000).build();
        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost post = new HttpPost();
        post.setConfig(requestConfig);
        post.setURI(new URI(""));
        post.setEntity(new StringEntity("", ContentType.create(MimeType.JSON, Charset.forName("UTF-8"))));

        try {
            CloseableHttpResponse response = client.execute(post);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String content = EntityUtils.toString(response.getEntity());
                System.out.println(content);
            }
        } finally {
            post.releaseConnection();
            client.close();
        }
    }
}
